package cn.com.chengmandian.bike.service.impl;

import cn.com.chengmandian.bike.model.EbDeviceBike;
import cn.com.chengmandian.bike.model.EbRideHelmet;
import cn.com.chengmandian.bike.mapper.EbRideHelmetMapper;
import cn.com.chengmandian.bike.model.EbRideOrder;
import cn.com.chengmandian.bike.service.IEbDeviceBikeService;
import cn.com.chengmandian.bike.service.IEbRideHelmetService;
import cn.com.chengmandian.core.common.constants.CommonConstant;
import cn.com.chengmandian.core.redis.cache.AgentParamCacheService;
import cn.com.chengmandian.core.redis.service.RedisService;
import cn.com.chengmandian.core.web.exceptions.BadResponseException;
import cn.com.chengmandian.core.web.pojo.BikeAreaParam;
import cn.com.chengmandian.service.bike.constants.BikeHelmetInstallEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 电单车运营-骑行订单头盔表 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2024-11-13
 */
@Slf4j
@Service
public class EbRideHelmetServiceImpl extends ServiceImpl<EbRideHelmetMapper, EbRideHelmet> implements IEbRideHelmetService {

    @Autowired
    private AgentParamCacheService agentParamCacheService;

    @Autowired
    private IEbDeviceBikeService deviceBikeService;

    @Autowired
    private RedisService redisService;

    @Override
    public EbRideHelmet getByOrder(Integer orderId) {
        return this.getOne(new LambdaQueryWrapper<EbRideHelmet>()
                .eq(EbRideHelmet::getOrderId, orderId)
                .orderByDesc(EbRideHelmet::getId)
                .last(" limit 0,1"));
    }

    @Override
    public void createRecord(Integer orderId, EbDeviceBike deviceBike) {
        EbRideHelmet rideHelmet = new EbRideHelmet();
        rideHelmet.setOrderId(orderId);
        rideHelmet.setBikeId(deviceBike.getId());
        rideHelmet.setBorrowHelmetExist(deviceBike.getHelmetExist() == null? -1 : deviceBike.getHelmetExist());
        rideHelmet.setBorrowHelmetInstall(deviceBike.getHelmetInstall() == null? -1 : deviceBike.getHelmetInstall());
        rideHelmet.setBorrowHelmetStatus(deviceBike.getHelmetStatus() == null? -1 : deviceBike.getHelmetStatus());
        this.save(rideHelmet);
    }

    @Override
    public void returnUpdate(Integer id, EbDeviceBike deviceBike) {
        this.update(new LambdaUpdateWrapper<EbRideHelmet>()
                .set(EbRideHelmet::getReturnHelmetExist, deviceBike.getHelmetExist() == null? -1 : deviceBike.getHelmetExist())
                .set(EbRideHelmet::getReturnHelmetInstall, deviceBike.getHelmetInstall() == null? -1 : deviceBike.getHelmetInstall())
                .set(EbRideHelmet::getReturnHelmetStatus, deviceBike.getHelmetStatus() == null? -1 : deviceBike.getHelmetStatus())
                .eq(EbRideHelmet::getId, id));
    }

    @Override
    public Boolean checkReturnBikeHelmet(BikeAreaParam bikeAreaParam, EbDeviceBike deviceBike, EbRideOrder rideOrder) {
        // 1.无需佩戴头盔
        if (!bikeAreaParam.getUserNeedWearHelmet()) return true;

        // 2.无需检查头盔在位
        if (!bikeAreaParam.getUserCloseLockCheckHelmetReignStatus()) return true;

        // 骑行订单头盔锁记录
        EbRideHelmet rideHelmet = null;
        if (rideOrder != null) {
            rideHelmet = this.getByOrder(rideOrder.getId());
        } else {
            log.error("checkReturnBikeHelmet未找到订单：{}, {}, {}", deviceBike.getBikeNo(), deviceBike.getId(), deviceBike.getStatus());
        }
        if(rideHelmet != null) {
            this.returnUpdate(rideHelmet.getId(), deviceBike);
        }

        // 3、未安装头盔，无需检查
        if (deviceBike.getHelmetInstall().equals(BikeHelmetInstallEnum.NO.getCode())) return true;

        // 4、校验头盔锁解锁的redis锁
//        String redisKey = "CMD:BIKE:ORDER:HELMET:" + deviceBike.getId();
//        if (redisService.hasKey(redisKey)) {
//            throw new BadResponseException("开锁操作太频繁，请稍候再试");
//        }

        if (rideHelmet != null) {
            // 取车时头盔在位
            if (rideHelmet.getBorrowHelmetExist().equals(CommonConstant.ONE)) {
                // 5、头盔是解锁状态
//                if (deviceBike.getHelmetStatus().equals(CommonConstant.ONE)) return false;
                // 6、头盔不在位
                if (!deviceBike.getHelmetExist().equals(CommonConstant.ONE)) return false;
            }
        } else {
            // 5、头盔是解锁状态
//            if (deviceBike.getHelmetStatus().equals(CommonConstant.ONE)) return false;
            // 6、头盔不在位
            if (!deviceBike.getHelmetExist().equals(CommonConstant.ONE)) return false;
        }
        return true;
    }
}
